/**
 * 
 */
package com.xb.cloud.product.merchant.inner.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.JsonUtils;
import com.xb.cloud.product.merchant.inner.service.ProductSkuExtDescInnerService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.impl.AbstractService;
import com.xb.cloud.product.service.business.model.ProductSkuExtDescription;
import com.xb.cloud.product.service.business.model.ProductSkuExtDescriptionExample;
import com.xb.cloud.product.service.business.model.mapper.ProductSkuExtDescriptionMapper;

/**
 * 富文本服务类.
 * 
 * @author kexue
 *
 */
@Service
public class ProductSkuExtDescInnerServiceImpl extends AbstractService implements ProductSkuExtDescInnerService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ProductSkuExtDescInnerServiceImpl.class);

	@Resource
	private ProductSkuExtDescriptionMapper skuExtDescmapper;

	/**
	 * 保存sku的文本描述内容
	 */
	@Override
	public Long createSkuExtDesc(String detailContent, Long productSkuId,
			EndMerchantUserBo endMerchantUserBo) {

		ProductSkuExtDescription skuExtDesc = new ProductSkuExtDescription();
		skuExtDesc.setCompanyId(endMerchantUserBo.getCompanyId());
		Long generateKey = ipKeyGenerator.generateKey();
		skuExtDesc.setId(generateKey);
		skuExtDesc.setSkuId(productSkuId);
		skuExtDesc.setCompanyId(endMerchantUserBo.getCompanyId());
		skuExtDesc.setDetailContent(detailContent);

		skuExtDesc.setCreateTime(timeService.getDate());
		skuExtDesc.setCreateUserId(endMerchantUserBo.getId());
		skuExtDesc.setCreateUserName(endMerchantUserBo.getLoginName());
		 int insertSelective = skuExtDescmapper.insertSelective(skuExtDesc);
		if (insertSelective <= 0) {
			String message = ("插入sku price失败" + JsonUtils.beanToJson(skuExtDesc));
			LOGGER.error(message);
			throw new RuntimeException(message); 
		}
		return generateKey;

	}
	
	/**
	 * 修改sku的文本描述内容
	 */
	@Override
	public void updateSkuExtDesc(String detailContent, Long productSkuId,
			EndMerchantUserBo endMerchantUserBo) {
		//更新条件
		ProductSkuExtDescriptionExample example=new ProductSkuExtDescriptionExample();
		example.createCriteria()
			.andSkuIdEqualTo(productSkuId)
			.andCompanyIdEqualTo(endMerchantUserBo.getCompanyId());	
		//更新数据
		List<ProductSkuExtDescription> skuExtDescs=skuExtDescmapper.selectByExample(example);
		ProductSkuExtDescription desc;
		int affected=0;
		if(skuExtDescs.size()==0){
			desc = new ProductSkuExtDescription();
			desc.setId(ipKeyGenerator.generateKey());
			desc.setCompanyId(endMerchantUserBo.getCompanyId());
			desc.setSkuId(productSkuId);
			desc.setDetailContent(detailContent);
			desc.setCreateTime(timeService.getDate());
			desc.setCreateUserId(endMerchantUserBo.getId());
			desc.setCreateUserName(endMerchantUserBo.getLoginName());
			//插入
			affected = skuExtDescmapper.insertSelective(desc);
		}else{
			desc=skuExtDescs.get(0);
			desc.setDetailContent(detailContent);
			desc.setUpdateTime(timeService.getDate());
			desc.setUpdateUserId(endMerchantUserBo.getId());
			desc.setUpdateUserName(endMerchantUserBo.getLoginName());
			//开始更新
			affected = skuExtDescmapper.updateByExampleSelective(desc, example);
		}
		
		if (affected != 1) {
			String message = ("更新sku price失败" + JsonUtils.beanToJson(desc));
			LOGGER.error(message);
			throw new RuntimeException(message); 
		}
	}

}
